package com.wjm.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wjm.entity.Record;
import com.wjm.entity.StuRecInfo;
import com.wjm.mapper.interf.RecordMapper;
import com.wjm.service.interf.RecordServiceInterface;
import com.wjm.util.CacheUtil;
import com.wjm.util.JsonUtil;
import com.wjm.util.StringUtil;

@Service("recordService")
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class RecordServiceImpl implements RecordServiceInterface {

	@Autowired
	private RecordMapper recordMapper;

	@Resource(name = "classroomService")
	private ClassroomServiceImpl classroomService;

	@Override
	public boolean save(Record object) throws Exception {
		boolean result = false;

		// 需要线程安全
		if (CacheUtil.getRecords().get(object.getDeskId()) == null) {
			synchronized (this) {
				if (CacheUtil.getRecords().get(object.getDeskId()) == null) {
					CacheUtil.getRecords().put(object.getDeskId(), object);
					object.setRecStatus(1);
					if (recordMapper.save(object) > 0) {
						result = true;
					} else {
						CacheUtil.getRecords().remove(object.getDeskId());
					}
				}
			}
		}

		return result;
	}

	@Override
	public boolean delete(int objectId) throws Exception {
		boolean result = false;
		if (recordMapper.delete(objectId) > 0) {
			result = true;
		}
		return result;

	}

	@Override
	public boolean update(Record object) throws Exception {
		boolean result = false;
		if (recordMapper.update(object) > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public List<Record> queryByPage(int current, int number, Record record) throws Exception {
		current = number * (current - 1);
		return recordMapper.selectByPage(current, number, record);
	}

	@Override
	public int queryTotalNum(Record record) throws Exception {
		return recordMapper.selectTotalNum(record);
	}

	@Override
	public List<Record> queryActive() throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat(StringUtil.TODAY_TIME_FORMAT);
		String today = sdf.format(new Date());
		return recordMapper.selectActive(today);
	}

	@Override
	public int queryCancel(int stuId) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat(StringUtil.TODAY_TIME_FORMAT);
		String today = sdf.format(new Date());
		return recordMapper.selectCancel(stuId, today);
	}

	@Override
	@Scheduled(cron = "0 0/2 * * * ?")
	public void checkRecord() throws Exception {
		Map<Integer, Record> records = CacheUtil.getRecords();
		if (records != null) {
			int hour = Integer.parseInt(CacheUtil.getProperties().getProperty(StringUtil.OVER_TIME)) * 60000;
			long now = new Date().getTime();
			SimpleDateFormat sdf = new SimpleDateFormat(StringUtil.TIME_FORMAT);
			List<Integer> deskIds = new ArrayList<>();
			for (Map.Entry<Integer, Record> entry : records.entrySet()) {
				Record rec = entry.getValue();
				System.out.println(JsonUtil.getJson(rec));
				// 如果是未入座则检查是否超时
				if (rec.getRecStatus() == 1) {
					long cr = sdf.parse(rec.getRecDate()).getTime() + hour;
					System.out.println(cr + "  " + now + "  " + hour + "  " + sdf.parse(rec.getRecDate()));
					if (cr < now) {
						rec.setRecStatus(3);
						if (recordMapper.update(rec) > 0) {
							classroomService.checkUse(rec.getCrId());
							deskIds.add(rec.getDeskId());
							// records.remove(rec.getDeskId());
							// 修改缓存中当天预约信息
							StuRecInfo info = CacheUtil.getStuRecInfos().get(rec.getStuNo());
							info.setCancel(info.getCancel() + 1);
							info.setIsHave(false);
						}
					}
				}
			}
			for (Integer deskId : deskIds) {
				records.remove(deskId);
			}
		} else {
			records = new HashMap<>();
			CacheUtil.setRecords(records);
		}
	}

}
